package com.exchange.consumer;

import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.GeoUnit;

import java.util.List;

public class RedisGeoExample {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost", 6379);

        // 添加地理位置数据
        jedis.geoadd("locations", 116.405285, 39.904989, "北京");
        jedis.geoadd("locations", 121.473701, 31.230416, "上海");
        jedis.geoadd("locations", 113.264385, 23.12911, "Guangzhou");
        jedis.geoadd("locations", 113.937447, 22.539968, "Shenzhen");

        // 使用 GeoUnit 来确定单位
        List<GeoRadiusResponse> nearbyLocations = jedis.georadius("locations", 116.405285, 39.904989, 500, GeoUnit.KM);

        System.out.println("附近的地点：");
        for (GeoRadiusResponse location : nearbyLocations) {
            System.out.println(location.getMemberByString() + " - 距离中心点：" + location.getDistance() + " km");
        }

        // 计算两个地点之间的距离
        Double distance = jedis.geodist("locations", "北京", "上海", GeoUnit.KM);
        System.out.println("北京到上海的距离：" + distance + " km");

        jedis.close();
    }
}


